package com.gzhryc.shared_device.oem.h5.mng.controllers.member;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.DateBranchHelper;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.EPayState;
import com.gzhryc.shared_device.base.enums.EPayType;
import com.gzhryc.shared_device.base.models.OrderLogContent;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberRechargeOrder;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberRechargeOrderLog;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberRechargeOrderEnum;
import com.gzhryc.shared_device.oem.code.member.services.MemberRechargeOrderLogService;
import com.gzhryc.shared_device.oem.code.member.services.MemberRechargeOrderService;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberRechargeOrderSearch;
import com.gzhryc.shared_device.common.payment.dao.db.AliRefundOrder;
import com.gzhryc.shared_device.common.payment.dao.db.AlipayOrder;
import com.gzhryc.shared_device.common.payment.dao.db.WXPayOrder;
import com.gzhryc.shared_device.common.payment.dao.db.WXRefundOrder;
import com.gzhryc.shared_device.common.payment.services.AliRefundOrderService;
import com.gzhryc.shared_device.common.payment.services.AlipayOrderService;
import com.gzhryc.shared_device.common.payment.services.WXPayOrderService;
import com.gzhryc.shared_device.common.payment.services.WXRefundOrderService;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 会员充值订单页面控制器
 * @version 2022-10-13
 */
public class MemberRechargeOrderController {

    static Logger log = Logger.getLogger(MemberRechargeOrderController.class);

    @Route(value = { "/member/recharge/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("payTypeList", EPayType.values());
        request.setAttribute("payStateList", EPayState.values());
        request.setAttribute("stateList", MemberRechargeOrderEnum.State.values());
        request.setAttribute("dateBranchItemList", DateBranchHelper.self().getYearDateBranchItems());
        return "/member/recharge/list.html";
    }

    @Route(value = "/member/recharge/list.data")
    public ListResult<MemberRechargeOrder> listData(HttpServletRequest request) {
        ListResult<MemberRechargeOrder> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        MemberRechargeOrderSearch search = WebUtils.getParameter(request, MemberRechargeOrderSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        Date dateBranch = new Date();
        if(StringTools.isNotBlank(search.getDateBranchId())){
            dateBranch = DateBranchHelper.getDateByItemId(search.getDateBranchId());
        }

        result.getBody().setTotal(MemberRechargeOrderService.self(dateBranch).countBySearch(search));
        List<MemberRechargeOrder> memberList = MemberRechargeOrderService.self(dateBranch).findBySearch(search,pageNo,pageLimit);
        if(memberList != null && memberList.size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(memberList);
        }
        return result;
    }

    @Route(value = { "/member/recharge/info.html" })
    public String info(HttpServletRequest request) {
        String tradeNo = WebUtils.getString(request,"tradeNo");
        Date createDate = WebUtils.getDate(request,"createDate");
        if(createDate == null){
            createDate = new Date();
        }
        MemberRechargeOrder rechargeOrder = MemberRechargeOrderService.self(createDate).getByTradeNo(tradeNo);
        if(rechargeOrder != null) {
            WebUtils.setAttribute(request, rechargeOrder);
            if(EPayType.WXPay.index().equals(rechargeOrder.getPayType())) {
	            List<WXPayOrder> payOrderList = WXPayOrderService.self(rechargeOrder.getCreateDate()).findByBusinessTradeNo(tradeNo);
	            if(payOrderList != null && payOrderList.size() > 0){
	                request.setAttribute("payOrderList",payOrderList);
	            }
	            
	            List<WXRefundOrder> refundOrderList = WXRefundOrderService.self(rechargeOrder.getCreateDate()).findByBusinessTradeNo(tradeNo);
	            if(refundOrderList != null && refundOrderList.size() > 0){
	                request.setAttribute("refundOrderList",refundOrderList);
	            }
            }else if(EPayType.Alipay.index().equals(rechargeOrder.getPayType())) {
            	List<AlipayOrder> payOrderList = AlipayOrderService.self(rechargeOrder.getCreateDate()).findByBusinessTradeNo(tradeNo);
	            if(payOrderList != null && payOrderList.size() > 0){
	                request.setAttribute("payOrderList",payOrderList);
	            }
	            
	            List<AliRefundOrder> refundOrderList = AliRefundOrderService.self(rechargeOrder.getCreateDate()).findByBusinessTradeNo(tradeNo);
	            if(refundOrderList != null && refundOrderList.size() > 0){
	                request.setAttribute("refundOrderList",refundOrderList);
	            }
            }
        }
        return "/member/recharge/info.html";
    }

    @Route(value = { "/member/recharge/operate/list.data" })
    public ListResult<OrderLogContent.LogItem> operateListData(HttpServletRequest request) {
        ListResult<OrderLogContent.LogItem> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        String tradeNo = WebUtils.getString(request,"tradeNo");
        Date createDate = WebUtils.getDate(request,"createDate");
        if(createDate == null){
            createDate = new Date();
        }
        MemberRechargeOrderLog rechargeOrderLog = MemberRechargeOrderLogService.self(createDate).getByTradeNo(tradeNo);
        if(rechargeOrderLog != null && JsonTools.isJsonStr(rechargeOrderLog.getContent())){
            OrderLogContent logContent = JsonTools.fromJson(rechargeOrderLog.getContent(),OrderLogContent.class);
            if(logContent != null && logContent.getItems() != null && logContent.getItems().size() > 0){
                result.setCode(DtoConstants.SUCCESS,null);
                result.getBody().setData(logContent.getItems() );
            }
        }
        return result;
    }
}
